<?php
require_once FOREGROUND_COMMON_ACTION_SERVICE_DIR . '/ajax.php';

class foreground_cart_delete_action_service extends foreground_ajax_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {	     		     
	    //var_dump(__METHOD__); exit();
	    //var_dump($_SESSION); exit();
	
	    $result = array('status' => 0, 'message' => '', 'data' => array());
	
	    //var_dump($_POST); exit();
	    $id = init_user_data_key($_POST, 'id', 'int');
	
	    $action_data = wwy_action::$data;
	    $session_member_info = $action_data['session_member_info'];
	    $member_id = $session_member_info['id'];
	
        if(empty($id)){
            $result['status']  = -2;
            $result['message'] = '删除失败';
        }
        
        if($result['status'] == 0){            
            $model_foreground_cart = wwy_resource_container::get_model_foreground_cart();
            //var_dump($model_foreground_cart); exit();
            
            //--1>【删除不用检查】。检查当前商品规格是否存在，若存在，即未下架，则继续操作；否则提示【当前商品规格已下架，将重新加载页面，清除已下架的商品规格】，然后重新加载页面，清除已下架的商品规格
            //2>检查是否存在已下架的商品规格，若存在，则提示【删除成功。您的购物车存在已下架的商品规格，将重新加载页面，清除已下架的商品规格】，然后重新加载页面，清除已下架的商品规格
            
            $where      = array(
                'id'        => $id,
                'member_id' => $member_id,
            );
            $cart_row = $model_foreground_cart->fetch_row($where);
            //var_dump($cart_row); exit();
        
            if(empty($cart_row)){
                $result['status']  = -3;
                $result['message'] = '删除失败';
            }else{
                $change_item_total =  - $cart_row['good_price'] * $cart_row['good_number'];
                 
                $where = array( 'ids' => array($id));
                $delete_result = $model_foreground_cart->delete($where);
                if(!$delete_result){
                    $result['status']  = -4;
                    $result['message'] = '删除失败';
                }else{
                    $check_cart_list_good_spec_result = $model_foreground_cart->check_cart_list_good_spec($member_id);
                    if($check_cart_list_good_spec_result['status'] < 0){
                        //检查是否存在已下架的商品规格，若存在，则提示【删除成功。您的购物车存在已下架的商品规格，将重新加载页面，清除已下架的商品规格】，然后重新加载页面，清除已下架的商品规格
                        $result['status']  = -404;
                        $result['message'] = '删除成功。您的购物车存在已下架的商品规格，将重新加载页面，清除已下架的商品规格';
                    }else{                                              
                        $result['status']  = 1;
                        $result['message'] = '删除成功';
                        $result['data']['change_item_total'] = number_format($change_item_total, 2, '.', '');
                        
                        self::add_view_data('change_item_total', $result['data']['change_item_total']);
                    }
                }
            }
        }
        
        self::set_view_status($result['status']);
        self::set_view_message($result['message']);
    }
}